﻿using UnityEngine;

public class PlayerPrimaryAttack: PlayerStateBase
{
    public PlayerPrimaryAttack(ControllerBase controller, StateMachineBase stateMachine, string stateName,string 
        animatorParameter) 
        : base(controller, stateMachine, stateName,animatorParameter)
    {
    }

    private int comboCount;
    private float  lastAttackTime;
    
    public override void Enter()
    {
        base.Enter();
        
        if (comboCount>2||Time.time-lastAttackTime>Setting.comboDuration)
        {
            comboCount = 0;
        }
        //运行攻击之间转向
        float attackDir = controller.faceDir;
        if (SelfController.moveX!=0)
        {
            attackDir = SelfController.moveX;
        }
        //给攻击设置初始的力
        controller.SetVelocity(new Vector2(SelfController.comboXXVelocity[comboCount].x*attackDir
            ,SelfController.comboXXVelocity[comboCount].y));
        controller.mAnimator.SetInteger(Setting.AnimatorComboCounter, comboCount);
    }

    public override void Exit()
    {
        base.Exit();
        comboCount++;
        lastAttackTime = Time.time;
        
        //设置为busy防止快速触发move状态
        controller.StartBusy(0.15f);
    }

    public override void Update()
    {
        base.Update();
        if (stateDuration>Setting.comboCanMoveTime)//移动时间超过0.12s，则禁止移动
        {
            controller.SetVelocity(Vector2.zero);
        }
        if (triggerFinish)
        {
            stateMachine.ChangeState(Setting.idle);
            return;
        }
        
        if (triggerCancel)// 攻击后摇，可进行下次攻击
        {
            if (Input.GetMouseButtonDown(0))
            {
                stateMachine.ChangeState(Setting.primaryAttack);
                return;
            }
        }
    }
}